package com.example.administrator.szb.fragments.fragment_forTab;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bumptech.glide.Glide;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.AskMannagerActivity;
import com.example.administrator.szb.activity.GZXYActivity;
import com.example.administrator.szb.activity.GuanZhuActivity;
import com.example.administrator.szb.activity.JFGLActivity;
import com.example.administrator.szb.activity.JGGLYESActivity;
import com.example.administrator.szb.activity.MessageActivity;
import com.example.administrator.szb.activity.MyInfoActivity;
import com.example.administrator.szb.activity.MySuPeiActivity;
import com.example.administrator.szb.activity.SZActivity;
import com.example.administrator.szb.activity.ShouCangTmp;
import com.example.administrator.szb.activity.XMGLActivity;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.layout.RZCenterActivity2;
import com.example.administrator.szb.activity.luyan.MyLuYanActivity;
import com.example.administrator.szb.activity.myyewu.MyYeWuActivity;
import com.example.administrator.szb.activity.teseservice.TeseActivity;
import com.example.administrator.szb.bean.UserInfo2;
import com.example.administrator.szb.common.SetStatusBar;
import com.example.administrator.szb.fragments.base.MVPBaseFragment;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.SPUtils;

import java.util.HashMap;
import java.util.Map;


/**顾问
 * Created by stone on 2017/12/29.
 */

public class FragmentMy3 extends MVPBaseFragment implements View.OnClickListener{

    private ImageView head_img;
    private TextView nickname;
    private TextView qianming;
    private TextView tv_status;
    private android.support.v7.widget.CardView cardView;
    private LinearLayout my_project_ll;
    private TextView my_project_text;
    private LinearLayout my_ask_ll;
    private TextView my_ask_text;
    private LinearLayout my_collection_ll;
    private TextView my_collection_text;
    private LinearLayout my_guanzhu_ll;
    private TextView my_guanzhu_text,no_renzhang_tv;
    private LinearLayout no_renzhang_ll;
    private TextView no_renzhang_btn;
    private LinearLayout my_yewu_ll;
    private TextView my_yewu_noadd;
    private LinearLayout my_jigou_ll;
    private TextView my_jigou_nofinish;
    private LinearLayout my_jf_ll;
    private LinearLayout my_message_ll,go_info_ll;
    private LinearLayout about_us;
    private LinearLayout setting,luyan,tese,supei_ll;

    private SwipeRefreshLayout swiperefreshlayout;
    UserInfo2 userInfo2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine_unlogin2,null);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(View view) {
        no_renzhang_tv = view.findViewById(R.id.no_renzhang_tv);
        supei_ll = view.findViewById(R.id.supei_ll);
        supei_ll.setOnClickListener(this);
        SetStatusBar.setBar(view,getContext());
        luyan = view.findViewById(R.id.luyan);
        tese = view.findViewById(R.id.tese);
        go_info_ll = view.findViewById(R.id.go_info_ll);
        swiperefreshlayout = view.findViewById(R.id.swiperefreshlayout);
        head_img = (ImageView) view.findViewById(R.id.head_img);
        nickname = (TextView) view.findViewById(R.id.nickname);
        qianming = (TextView) view.findViewById(R.id.qianming);
        tv_status = (TextView) view.findViewById(R.id.tv_status);
        cardView = (android.support.v7.widget.CardView) view.findViewById(R.id.cardView);
        my_project_ll = (LinearLayout) view.findViewById(R.id.my_project_ll);
        my_project_text = (TextView) view.findViewById(R.id.my_project_text);
        my_ask_ll = (LinearLayout) view.findViewById(R.id.my_ask_ll);
        my_ask_text = (TextView) view.findViewById(R.id.my_ask_text);
        my_collection_ll = (LinearLayout) view.findViewById(R.id.my_collection_ll);
        my_collection_text = (TextView) view.findViewById(R.id.my_collection_text);
        my_guanzhu_ll = (LinearLayout) view.findViewById(R.id.my_guanzhu_ll);
        my_guanzhu_text = (TextView) view.findViewById(R.id.my_guanzhu_text);
        no_renzhang_ll = (LinearLayout) view.findViewById(R.id.no_renzhang_ll);
        no_renzhang_btn = (TextView) view.findViewById(R.id.no_renzhang_btn);
        my_yewu_ll = (LinearLayout) view.findViewById(R.id.my_yewu_ll);
        my_yewu_noadd = (TextView) view.findViewById(R.id.my_yewu_noadd);
        my_jigou_ll = (LinearLayout) view.findViewById(R.id.my_jigou_ll);
        my_jigou_nofinish = (TextView) view.findViewById(R.id.my_jigou_nofinish);
        my_jf_ll = (LinearLayout) view.findViewById(R.id.my_jf_ll);
        my_message_ll = (LinearLayout) view.findViewById(R.id.my_message_ll);
        about_us = (LinearLayout) view.findViewById(R.id.about_us);
        setting = (LinearLayout) view.findViewById(R.id.setting);

    }


    @Override
    protected void initEvent() {
        swiperefreshlayout.setRefreshing(true);
        swiperefreshlayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 设置下拉进度的主题颜色
        swiperefreshlayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);
// 下拉时触发SwipeRefreshLayout的下拉动画，动画完毕之后就会回调这个方法
        swiperefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                requestUserInfo();

            }
        });

        //tv_status my_project_ll my_ask_ll my_collection_ll my_guanzhu_ll no_renzhang_btn
        //my_yewu_ll my_jigou_ll my_jf_ll my_message_ll about_us setting
        tv_status.setOnClickListener(this);
        my_project_ll.setOnClickListener(this);
        setting.setOnClickListener(this);
        about_us.setOnClickListener(this);
        my_message_ll.setOnClickListener(this);
        my_jf_ll.setOnClickListener(this);
        my_jigou_ll.setOnClickListener(this);
        my_yewu_ll.setOnClickListener(this);
        no_renzhang_btn.setOnClickListener(this);
        my_guanzhu_ll.setOnClickListener(this);
        my_collection_ll.setOnClickListener(this);
        my_ask_ll.setOnClickListener(this);
        go_info_ll.setOnClickListener(this);
        luyan.setOnClickListener(this);
        tese.setOnClickListener(this);

    }

    @Override
    protected void initData() {
        if (SPUtils.getUser()!=null){
            userInfo2 = SPUtils.getUser();
            initUser();
        }
        //requestUserInfo();
    }
    private void initUser(){
        Glide.with(context).load(userInfo2.getHeadimg()).error(R.mipmap.default_header_s).into(head_img);
        nickname.setText(userInfo2.getNick_name());
        qianming.setText("公司名称："+userInfo2.getIntro());
        if (userInfo2.getStatus()>1&&userInfo2.getStatus()<5){
            qianming.setText(userInfo2.getCompany());
        }
        String str = "";
        switch (userInfo2.getStatus()){
            case 1:
                str="未认证";
                no_renzhang_ll.setVisibility(View.VISIBLE);
                break;
            case 2:
                str="审核中";
                no_renzhang_ll.setVisibility(View.GONE);
                break;
            case 3:
                no_renzhang_ll.setVisibility(View.GONE);
                str="审核通过";

                break;
            case 4:
                str="未通过";
                no_renzhang_tv.setText("您的认证未通过审核，请重新认证");
                no_renzhang_ll.setVisibility(View.VISIBLE);

                break;
            case 5:
                str="禁用";
                break;
            default:
                str="尚未认证";
                break;
        }
        tv_status.setText(str);
        if (userInfo2.getIsBus()==1){
            my_yewu_noadd.setVisibility(View.GONE);
        }else {
            my_yewu_noadd.setVisibility(View.VISIBLE);
        }
        if (userInfo2.getMiIfo()==1){
            my_jigou_nofinish.setVisibility(View.GONE);
        }else {
            my_jigou_nofinish.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()){
            case R.id.supei_ll:
                intent = new Intent(context, MySuPeiActivity.class);
                startActivity(intent);
                break;
            case R.id.tese:
                intent = new Intent(getActivity(), TeseActivity.class);
                intent.putExtra("showRight",true);
                startActivity(intent);
                break;
            case R.id.luyan:
                intent = new Intent(getActivity(), MyLuYanActivity.class);
                startActivity(intent);
                break;
            case R.id.setting:
                startActivity(new Intent(getActivity(), SZActivity.class));
                break;
            case R.id.go_info_ll:
            case R.id.tv_status:
                if (userInfo2==null){
                    return;
                }
                 intent = new Intent(getActivity(), MyInfoActivity.class);
                intent.putExtra("type",1);
                intent.putExtra("status",userInfo2.getStatus());
                startActivity(intent);
                break;
            case R.id.my_project_ll:
                startActivity(new Intent(getActivity(), XMGLActivity.class));
                break;
            case R.id.my_ask_ll:
                //
                startActivity(new Intent(getActivity(), AskMannagerActivity.class));
                break;
            case R.id.my_collection_ll:
                startActivity(new Intent(getActivity(), ShouCangTmp.class));
                break;
            case R.id.my_guanzhu_ll:
                startActivity(new Intent(getActivity(), GuanZhuActivity.class));
                break;
            case R.id.no_renzhang_btn:
                if (userInfo2==null){
                    return;
                }
                 intent = new Intent(getActivity(), RZCenterActivity2.class);
                intent.putExtra("rzStatu",userInfo2.getStatus());
                startActivity(intent);
                break;
            case R.id.my_yewu_ll:
                if (userInfo2==null||userInfo2.getStatus()!=3){
                    DialogUtil.showToast(getActivity(),"认证通过后才可以进行业务管理哦");
                    return;
                }
                startActivity(new Intent(getActivity(), MyYeWuActivity.class));
                //startActivity(new Intent(getActivity(), YWGLYESActivity.class));
                break;
            case R.id.my_jigou_ll:
                if (userInfo2==null||userInfo2.getStatus()!=3){
                    DialogUtil.showToast(getActivity(),"认证通过后才可以完善机构信息哦");
                   return;
                }
                startActivity(new Intent(getActivity(), JGGLYESActivity.class));

                break;
            case R.id.my_jf_ll:
                startActivity(new Intent(getActivity(), JFGLActivity.class));
                break;
            case R.id.my_message_ll:
                startActivity(new Intent(getActivity(), MessageActivity.class));
                break;
            case R.id.about_us:
                Intent intent_web = new Intent(getActivity(),GZXYActivity.class);//额，
                intent_web.putExtra("title","关于我们");
                intent_web.putExtra("id",1);
                startActivity(intent_web);
                break;
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        requestUserInfo();
    }

    private void requestUserInfo(){
        Map<String,Object> params = new HashMap<>();
        params.put("code", HttpUtil.getParam());
        params.put("id", SPUtils.getUserId());
        HttpUtils.post(activity,URLAddress.URLBASE + URLAddress.URL_CENTER, params, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                swiperefreshlayout.setRefreshing(false);
                SPUtils.setDatas(SPUtils.USERS_INTO,str);
                userInfo2 = JSON.parseObject(str,new TypeReference<UserInfo2>(){});
                initUser();
            }

            @Override
            public void onFailure(int requestCode, String str) {
                swiperefreshlayout.setRefreshing(false);
                DialogUtil.showToast(context,str);
            }
        });
    }
}
